package com.example.java_second_hand_market;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;



public class BuyerDialog extends Stage {
    public BuyerDialog(Buyer by,int width,int height){
        Button exchangeBt = new Button("切换为商家");

        exchangeBt.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Stage stage = (Stage) exchangeBt.getScene().getWindow(); // 获取当前按钮所在的窗口
                stage.close();
                SellerDialog bd= new SellerDialog(new Seller(by.getName(),by.getAccount(),by.getPassword()),width,height);

            }
        });

        TabPane tabPane = new TabPane();

        Stage primaryStage = new Stage();
        // 创建一个 UserInfoDialog 实例
         // 假设有一个 User 类
        UserInfoDialog userInfoDialog = new UserInfoDialog(by,width,height);
        ChatDialog chatDialog = new ChatDialog(by,width,height);
        HistoryDialog historyDialog = new HistoryDialog(by,width,height);
        SearchDialog searchDialog = new SearchDialog(by,width,height);
        // 创建 Tab，并将 UserInfoDialog 的内容添加到 Tab 中
        Tab userTab = new Tab("用户信息");
        Tab chatTab = new Tab("聊天");
        Tab historyTab = new Tab("历史记录");
        Tab searchTab = new Tab("搜索");
        Tab changeTab=new Tab("切换为商家");
        chatTab.setContent(chatDialog.getBp());
        chatTab.setClosable(false);

        historyTab.setContent(historyDialog.getVbox());
        historyTab.setClosable(false);

        searchTab.setContent(searchDialog.getbp());
        searchTab.setClosable(false);

        userTab.setContent(userInfoDialog.getVBox());
        userTab.setClosable(false); // 设置 Tab 不可关闭
        HBox hBox = new HBox();
        hBox.getChildren().add(exchangeBt);
        hBox.setAlignment(Pos.CENTER);

        VBox vBox = new VBox();
        vBox.getChildren().add(hBox);
        vBox.setAlignment(Pos.CENTER);
        changeTab.setContent(vBox);
        changeTab.setClosable(false);
        // 将 Tab 添加到 TabPane 中
        tabPane.getTabs().add(userTab);
        tabPane.getTabs().add(chatTab);
        tabPane.getTabs().add(historyTab);
        tabPane.getTabs().add(searchTab);
        tabPane.getTabs().add(changeTab);
        tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {
            if (newTab ==historyTab) {
                historyDialog.refresh();
            }
        });
        // 创建场景并显示

        Scene scene = new Scene(tabPane, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.setTitle("二手市场管理系统");
        primaryStage.show();

    }
    };


